package com.alxy.day1.repository.specification;

import com.alxy.day1.entity.User;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Root;
import org.springframework.data.jpa.domain.Specification;

/**
 * @author 爱莉希雅
 * @version java 17
 */
@SuppressWarnings({"all"})
public class UserSpecifications {
    public static Specification<User> usernameContains(String username) {
        return (Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) -> {
            if (username == null || username.isEmpty()) {
                return null;
            }
            return cb.like(root.get("username"), "%" + username + "%");
        };
    }

    public static Specification<User> emailContains(String email) {
        return (Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) -> {
            if (email == null || email.isEmpty()) {
                return null;
            }
            return cb.like(root.get("email"), "%" + email + "%");
        };
    }
}